#define LOW_MEM 0x100000
#define PAGING_MEMORY (15*1024*1024)
#define PAGING_PAGES (PAGING_MEMORY>>12)
#define MAP_NR(addr) (((addr)-LOW_MEM)>>12)
#define USED 100

static long HIGH_MEMORY = 0;

static unsigned char mem_map[PAGING_PAGES] = {0, };    // 记录内存页面的使用情况

/**
 * 寻找空闲页
 * @return  0：没找到空闲页
 *          其他：空闲页地址
 */
unsigned long get_free_page(void)
{
    unsigned long __res;

    __asm__("std\n\trepne\n\tscasb\n\t"
        "jne 1f\n\t"
        "movb $1, 1(%%edi)\n\t"
        "sall $12, %%ecx\n\t"
        "addl %2, %%ecx\n\t"
        "movl %%ecx, %%edx\n\t"
        "movl $1024, %%ecx\n\t"
        "leal 4092(%%edx), %%edi\n\t"
        "rep\n\tstosl\n\t"
        "movl %%edx, %%eax\n"
        "1:"
        :"=a"(__res)
        :"0"(0), "i"(LOW_MEM), "c"(PAGING_PAGES),
        "D"(mem_map + PAGING_PAGES - 1)
        );
    return __res;
}

void mem_init(long start_mem, long end_mem)
{
    int i;

    HIGH_MEMORY = end_mem;
    for (i = 0; i < PAGING_PAGES; i++)
        mem_map[i] = USED;
    i = MAP_NR(start_mem);
    end_mem -= start_mem;
    end_mem >>= 12;            // 可分配内存的总页面数
    while (end_mem-- >0)
        mem_map[i++]=0;
}
